layui.use(['jquery','table','layer'],function () {
    var $ = layui.jquery;
    var table = layui.table;
    var layer = layui.layer;

    var commodityId = localStorage.getItem("commodityId");
    var userId = localStorage.getItem("userId");

    selectComment(commodityId,2);
    function selectComment(commodityId,type) {
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"commodityId":commodityId,"type":type},
            url:"/CourceComment/selectUserCourseCommentById",
            success:function (data) {
                var data = data.data;
                if(data.length==0){
                    $("#table").empty();
                    $("#table").append(
                        "<p id='contentt'>"+
                        "<img src='/images/moren.png' style='width:30px;height:30px;border-radius:50%;'>" +
                        "<span style='margin-left:10px'>"+"还没有评论呢..."+"</span>"
                        +"</th>"
                    );
                }else{
                    $("#table").empty();
                    for (var i = 0; i < data.length; i++) {
                        var show="";
                        if(data[i].status=="1"){
                            show="正常显示中";
                        }else if(data[i].status=="2"){
                            show="已拦截";
                        }else{
                            show="已删除";
                        }
                        $("#table").append(
                            "<p class='tableLeft'>"+
                            "<img src="+'http://'+data[i].acatarUrl+" id='avatarUrl' style='width:30px;height:30px;border-radius:50%;'>"+
                            "<span style='margin-left:1%'>"+data[i].trueName+"</span>"+
                            "<p style='margin-left:5%;font-size:16px;'>"+data[i].content+"</p>"+
                            "<p style='margin-left:5%;color: #999999;margin-top:10px;'>"+data[i].createDate+"" +
                            "<span style='color: #2F4056;margin-left:6%;'>"+show+"</span>"+
                            "</p>"
                            +"</p>"
                        );
                    }
                }
            }
        })
    };

    $("#tijiao").click(function () {
        var content = $("#content").val();
        var commentDate = new Date();
        commentDate = getTime(commentDate);
        if (content == null || content == "") {
            layer.msg("不能发送空的评论");
        } else {
            var json = {
                userId: userId,
                commodityId: commodityId,
                type: 2,
                userType: 2,
                status: 1,
                content: content,
                parentId: 0,
                commentDate: commentDate,
                display: 2,
                createId: userId,
                createDate: commentDate,
                updateId: userId,
                updateDate: commentDate
            };
            $.ajax({
                type: "post",
                dataType: "json",
                data: json,
                url: "/comment/addGoodsComment",
                success: function (data) {
                    if (data) {
                        layer.msg("添加评论成功");
                        selectComment();
                    } else {
                        layer.msg("添加评论失败");
                    }
                }
            })
        }
    });
        //顶部时间
        function getTime(date){
            var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
            var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
            var today = date.getDate(); //获取当前日(1-31)
            var hour = date.getHours(); //获取当前小时数(0-23)
            var minute = date.getMinutes(); //获取当前分钟数(0-59)
            var second = date.getSeconds(); //获取当前秒数(0-59)
            var nowTime;
            nowTime = year+'-'+fillZero(month)+'-'+fillZero(today)+' '+fillZero(hour)+':'+fillZero(minute)+':'+fillZero(second);
            return nowTime;
        };

        //时间的为零操作
        function fillZero(str){
            var realNum;
            if(str<10){
                realNum	= '0'+str;
            }else{
                realNum	= str;
            }
            return realNum;
        }
});